<template>
  <view class="mine-container" :style="{height: `${windowHeight}px`}">
    <!--顶部个人信息栏-->
    <view class="header-section">
      <view class="flex padding justify-between">
        <view class="flex align-center">
          <view v-if="!avatar" class="cu-avatar xl round bg-white">
            <view class="iconfont icon-people text-gray icon"></view>
          </view>
          <image v-if="avatar" @click="handleToAvatar" :src="avatar" class="cu-avatar xl round" mode="widthFix">
          </image>
          <view v-if="!name" @click="handleToLogin" class="login-tip">
            点击登录
          </view>
          <view v-if="name" @click="handleToInfo" class="user-info">
            <view class="u_title">
              用户名：{{ name }}
            </view>
          </view>
        </view>
        <view @click="handleToInfo" class="flex align-center">
          <text>个人信息</text>
          <view class="iconfont icon-right"></view>
        </view>
      </view>
    </view>

    <view class="content-section">
      <view class="mine-actions grid col-4 text-center">
        <view class="action-item" @click="handleJiaoLiuQun">
          <view class="iconfont icon-friendfill text-pink icon"></view>
          <text class="text">交流群</text>
        </view>
        <view class="action-item" @click="loadWechatQrcode">
          <view class="iconfont icon-service text-blue icon"></view>
          <text class="text">在线客服</text>
        </view>
        <!-- <view class="action-item" @click="handleBuilding">
          <view class="iconfont icon-community text-mauve icon"></view>
          <text class="text">反馈社区</text>
        </view> -->
        <view class="action-item" @click="loadWechatQrcode">
          <view class="iconfont icon-dianzan text-green icon"></view>
          <text class="text">点赞我们</text>
        </view>
      </view>

      <view class="menu-list">
        <view class="list-cell list-cell-arrow" @click="handleToEditInfo">
          <view class="menu-item-box">
            <view class="iconfont icon-user menu-icon"></view>
            <view>编辑资料</view>
          </view>
        </view>
        <view class="list-cell list-cell-arrow" @click="handleHelp">
          <view class="menu-item-box">
            <view class="iconfont icon-help menu-icon"></view>
            <view>常见问题</view>
          </view>
        </view>
        <view class="list-cell list-cell-arrow" @click="handleAbout">
          <view class="menu-item-box">
            <view class="iconfont icon-aixin menu-icon"></view>
            <view>关于我们</view>
          </view>
        </view>
        <view class="list-cell list-cell-arrow" @click="handleToSetting">
          <view class="menu-item-box">
            <view class="iconfont icon-setting menu-icon"></view>
            <view>应用设置</view>
          </view>
        </view>
      </view>

    </view>

    <!-- 微信客服二维码对话框 -->
    <view v-if="showWechatService" class="wechat-modal-mask" @click="showWechatService = false">
      <view class="wechat-modal" @click.stop>
        <view class="modal-title">在线客服</view>
        <view class="qrcode-container">
          <!-- 加载状态 -->
          <view v-if="isLoadingQrcode" class="loading-view">
            <view class="iconfont icon-loading"></view>
            <text>加载中...</text>
          </view>
          
          <!-- 错误状态 -->
          <view v-if="!isLoadingQrcode && !wechatQrcodeUrl" class="error-view">
            <view class="iconfont icon-error"></view>
            <text>二维码加载失败</text>
            <button class="retry-btn" @click="loadWechatQrcode">重试</button>
          </view>
          
          <!-- 二维码图片 -->
          <image 
            v-if="!isLoadingQrcode && wechatQrcodeUrl"
            :src="wechatQrcodeUrl" 
            mode="widthFix" 
            class="qrcode-img"
            alt="微信客服二维码"
          ></image>
          <text class="qrcode-desc">扫码添加微信客服</text>
        </view>
        <button class="close-btn" @click="showWechatService = false">关闭</button>
      </view>
    </view>
  </view>
</template>

<script>
	import { getWechatServiceQrcode } from '@/api/wechat.js'
  export default {
    data() {
      return {
        name: this.$store.state.user.name,
        showWechatService: false, // 控制微信客服对话框显示
        wechatQrcodeUrl: '', // 存储从后端获取的二维码URL
        isLoadingQrcode: false // 二维码加载状态
      }
    },
    computed: {
      avatar() {
        return this.$store.state.user.avatar
      },
      windowHeight() {
        return uni.getSystemInfoSync().windowHeight - 50
      }
    },
    methods: {
      handleToInfo() {
        this.$tab.navigateTo('/pages/mine/info/index')
      },
      handleToEditInfo() {
        this.$tab.navigateTo('/pages/mine/info/edit')
      },
      handleToSetting() {
        this.$tab.navigateTo('/pages/mine/setting/index')
      },
      handleToLogin() {
        this.$tab.reLaunch('/pages/login')
      },
      handleToAvatar() {
        this.$tab.navigateTo('/pages/mine/avatar/index')
      },
      handleHelp() {
        this.$tab.navigateTo('/pages/mine/help/index')
      },
      handleAbout() {
        this.$tab.navigateTo('/pages/mine/about/index')
      },
     /* handleJiaoLiuQun() {
        this.$modal.showToast('QQ群：①133713780(满)、②146013835(满)、③189091635')
      }, */
	  handleJiaoLiuQun() {
	    const qqGroups = 'QQ群：①133713780(满)、②146013835(满)、③189091635';
	    
	    // 复制到剪贴板
	    uni.setClipboardData({
	      data: qqGroups,
	      success: () => {
	        // 复制成功后显示提示
	        this.$modal.showToast('QQ群信息已复制');
	      },
	      fail: (err) => {
	        // 复制失败处理
	        console.error('复制失败', err);
	        this.$modal.showToast('复制失败，请手动复制');
	      }
	    });
	  },
      handleBuilding() {
        this.$modal.showToast('模块建设中~')
      },
      // 加载微信客服二维码
      async loadWechatQrcode() {
        // 显示对话框和加载状态
        this.showWechatService = true;
        this.isLoadingQrcode = true;
        
        try {
          // 调用后端API获取二维码
          const response = await getWechatServiceQrcode("客服");
          
          // 假设后端返回格式为 { code: 200, data: { url: '二维码图片URL' } }
          if (response.code === 200 && response.data) {
            this.wechatQrcodeUrl = response.data;
          } else {
            this.$modal.showToast('获取二维码失败');
            this.wechatQrcodeUrl = '';
          }
        } catch (error) {
          console.error('获取微信二维码失败:', error);
          this.$modal.showToast('网络错误，获取二维码失败');
          this.wechatQrcodeUrl = '';
        } finally {
          // 关闭加载状态
          this.isLoadingQrcode = false;
        }
      }
    }
  }
</script>

<style lang="scss" scoped>
  page {
    background-color: #f5f6f7;
  }

  .mine-container {
    width: 100%;
    height: 100%;


    .header-section {
      padding: 15px 15px 45px 15px;
      background-color: #3c96f3;
      color: white;

      .login-tip {
        font-size: 18px;
        margin-left: 10px;
      }

      .cu-avatar {
        border: 2px solid #eaeaea;

        .icon {
          font-size: 40px;
        }
      }

      .user-info {
        margin-left: 15px;

        .u_title {
          font-size: 18px;
          line-height: 30px;
        }
      }
    }

    .content-section {
      position: relative;
      top: -50px;

      .mine-actions {
        margin: 15px 15px;
        padding: 20px 0px;
        border-radius: 8px;
        background-color: white;

        .action-item {
          .icon {
            font-size: 28px;
          }

          .text {
            display: block;
            font-size: 13px;
            margin: 8px 0px;
          }
        }
      }
    }

    // 微信客服二维码对话框样式
    .wechat-modal-mask {
      position: fixed;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      background-color: rgba(0, 0, 0, 0.5);
      display: flex;
      justify-content: center;
      align-items: center;
      z-index: 999;
    }

    .wechat-modal {
      width: 80%;
      max-width: 300px;
      background-color: white;
      border-radius: 10px;
      padding: 20px;
      text-align: center;
    }

    .modal-title {
      font-size: 18px;
      font-weight: bold;
      margin-bottom: 20px;
      color: #333;
    }

    .qrcode-container {
      margin-bottom: 20px;
    }

    .loading-view, .error-view {
      padding: 40px 0;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
    }

    .loading-view .icon-loading, .error-view .icon-error {
      font-size: 30px;
      margin-bottom: 10px;
    }

    .loading-view .icon-loading {
      color: #3c96f3;
      animation: spin 1s linear infinite;
    }

    .error-view .icon-error {
      color: #ff4d4f;
    }

    .retry-btn {
      margin-top: 10px;
      padding: 5px 15px;
      background-color: #3c96f3;
      color: white;
      border: none;
      border-radius: 5px;
      font-size: 14px;
    }

    .qrcode-img {
      width: 200px;
      height: 200px;
      margin: 0 auto;
    }

    .qrcode-desc {
      display: block;
      margin-top: 10px;
      color: #666;
      font-size: 14px;
    }

    .close-btn {
      width: 100%;
      padding: 10px 0;
      background-color: #3c96f3;
      color: white;
      border: none;
      border-radius: 5px;
      font-size: 16px;
    }
  }

  @keyframes spin {
    from { transform: rotate(0deg); }
    to { transform: rotate(360deg); }
  }
</style>
